package com.grj.parking.drools.controller;

import com.grj.parking.drools.config.Result;
import com.grj.parking.drools.domain.ParkingLot;
import com.grj.parking.drools.domain.ParkingSpace;
import com.grj.parking.drools.domain.Vehicle;
import com.grj.parking.drools.service.ParkingLotService;
import com.grj.parking.drools.service.ParkingSpaceService;
import com.grj.parking.drools.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/data")
@Api(tags = "数据管理接口", description = "提供停车的功能")
@RequiredArgsConstructor
public class DataController {
    private final ParkingLotService parkingLotService;
    private final ParkingSpaceService parkingSpaceService;
    private final VehicleService vehicleService;

    @Autowired
    @Qualifier("entryMessageExecutor")
    private ExecutorService entryMessageExecutor;
    @ApiOperation(value = "批量插入数据", notes = "批量插入数据")
    @GetMapping("/data")
    public Result<Boolean> data() throws Exception {

        List<ParkingLot> parkingLotList = new ArrayList<>();

        for (int i = 1; i <= 1000; i++) {
            ParkingLot parkingLot = new ParkingLot();
            parkingLot.setId((long) i);
            parkingLot.setTenantId(1L);
            parkingLot.setLotName("lot" + i);
            parkingLot.setLotCode("code" + i);
            parkingLot.setAddress("address" + i);
            parkingLot.setTotalSpaces(1000);
            parkingLot.setAvailableSpaces(1000);
            parkingLot.setStatus(1);
            parkingLot.setCreateTime(new Date());
            // 佛山地区停车场经纬度随机值
            // 佛山经度范围：112.760000 - 113.660000
            // 佛山纬度范围：22.760000 - 23.660000
            Random random = new Random();
            double longitude = 112.760000 + (113.660000 - 112.760000) * random.nextDouble();
            double latitude = 22.760000 + (23.660000 - 22.760000) * random.nextDouble();
            parkingLot.setLongitude(BigDecimal.valueOf(longitude));
            parkingLot.setLatitude(BigDecimal.valueOf(latitude));
            parkingLotList.add(parkingLot);
        }
        parkingLotService.saveBatch(parkingLotList);
        System.out.println("ok");

        for (int i = 1; i <= 1000; i++) {
            List<ParkingSpace> parkingSpaceList = new ArrayList<>();
            List<Vehicle> vehicleList = new ArrayList<>();
            for (int j = 1; j <= 1000; j++) {
                ParkingSpace parkingSpace = new ParkingSpace();
                parkingSpace.setId((long) (i-1)*1000+ j);
                parkingSpace.setTenantId(1L);
                parkingSpace.setLotId((long) i);
                parkingSpace.setSpaceNumber("space" + parkingSpace.getId());
                parkingSpace.setSpaceType(1);
                parkingSpace.setStatus(0);
                parkingSpace.setCreateTime(new Date());
                parkingSpace.setUpdateTime(new Date());
                parkingSpaceList.add(parkingSpace);
                Vehicle vehicle = new Vehicle();
                vehicle.setId(parkingSpace.getId());
                vehicle.setLicensePlate("" + parkingSpace.getId());
                vehicle.setVehicleBrand("brand" + parkingSpace.getId());
                vehicle.setVehicleColor("color" + parkingSpace.getId());
                vehicle.setIsDefault(1);
                vehicle.setMemberId(parkingSpace.getTenantId());
                vehicle.setTenantId(1L);
                vehicle.setCreateTime(new Date());
                vehicle.setVehicleType(1);
                vehicle.setBindTime(new Date());
                vehicle.setCreateTime(new Date());
                vehicle.setUpdateTime(new Date());
                vehicleList.add(vehicle);
            }
            CompletableFuture.supplyAsync(() -> {
                log.info("批量保存车辆: {}", vehicleList.size());
                try {
                    vehicleService.saveBatch(vehicleList);
                }catch (Exception e){
                    log.error("批量保存车辆失败: {}", e.getMessage());
                }
                return null;
            },entryMessageExecutor);

            CompletableFuture.supplyAsync(() -> {
                log.info("批量保存车位: {}", vehicleList.size());
                parkingSpaceService.saveBatch(parkingSpaceList);
                return null;
            },entryMessageExecutor);
        }

        return Result.success(true);
    }


    @PostMapping("/add")
    public Result<List<ParkingLot>> list(ParkingLot parkingLot) {
        System.out.println(parkingLot);
        parkingLotService.save(parkingLot);
        return Result.success();
    }

    @PostMapping("/add2")
    public Result<String> list2() {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(1L);
        vehicle.setLicensePlate("plate" + 1L);
        vehicle.setVehicleType(1);
        vehicle.setMemberId(1L);
        vehicle.setUpdateTime(new Date());
        vehicle.setTenantId(1L);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        return Result.success();
    }


}
